package com.beordie.dao.impl;

import com.beordie.dao.IMiddleDao;
import com.beordie.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname MiddleDaoImpl
 * @Description TODO
 * @Date 2021/7/14 14:49
 * @Created 30500
 */
public class MiddleDaoImpl extends DBUtils implements IMiddleDao {
    @Override
    public int[] insert(int id, int[] pid) {
        int[] result = null;
        try {
            statement = super.getStatement(SQL_INSERT_PATCH);
            for (int i : pid) {
                statement.setObject(1, id);
                statement.setObject(2, i);
                statement.addBatch();
            }
            result = statement.executeBatch();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            result = null;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int delete(int id) {
        List<Integer> params = new ArrayList<>();
        params.add(id);

        int result = 0;
        try {
            result = super.update(SQL_DELETE_BY_NOTICE_ID, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }
}
